﻿#include "../../includes/common/Scheduledtasks.h"

#include <QDateTime>

Scheduledtasks::Scheduledtasks(QObject *parent)
    : QObject(parent),
      m_currentRunCount(0)
{
    connect(&m_taskTimer, &QTimer::timeout,
            this,
            &Scheduledtasks::onProcess_Scheduledtasks);
}

Scheduledtasks::~Scheduledtasks(void)
{

}

/**
 * @brief Scheduledtasks::startTask 开始任务
 */
void Scheduledtasks::startTask(void)
{
    m_taskTimer.start(10);
}

/**
 * @brief Scheduledtasks::stopTask 结束任务
 */
void Scheduledtasks::stopTask(void)
{
    m_taskTimer.stop();
}

bool Scheduledtasks::getSplitString(QString srcString,QString startstr,
                                    QString endstr,QVector<qint32> &decNum)
{
    if(srcString.isEmpty() || startstr.isEmpty() ||
            endstr.isEmpty())
        return false;

    if(srcString.lastIndexOf(startstr) < 0 || srcString.lastIndexOf(endstr) < 0)
        return false;

    QStringList pStringList = srcString.mid(1,srcString.length()-2).split(",");
    if(pStringList.isEmpty())
        return false;

    decNum.clear();

    for(int i=0;i<pStringList.size();i++)
        decNum.push_back(pStringList[i].toInt());

    return true;
}

/**
 * @brief Scheduledtasks::setRule 设置规则2
 * @param date
 * @param dayOfWeek
 * @param hour
 * @param minute
 * @param second
 */
void Scheduledtasks::setRule2(QString date,QString dayOfWeek,
                             QString hour,QString minute,
                             QString second)
{
    tagTaskRule pTaskRule;
    pTaskRule.runCount = 1;

    QVector<qint32> pdecNum;
    if(getSplitString(date,"[","]",pdecNum))
        pTaskRule.date = pdecNum;
    if(getSplitString(dayOfWeek,"[","]",pdecNum))
        pTaskRule.dayOfWeek = pdecNum;
    if(getSplitString(hour,"[","]",pdecNum))
        pTaskRule.hour = pdecNum;
    if(getSplitString(minute,"[","]",pdecNum))
        pTaskRule.minute = pdecNum;
    if(getSplitString(second,"[","]",pdecNum))
        pTaskRule.second = pdecNum;

    setRule(pTaskRule);
}

void Scheduledtasks::onProcess_Scheduledtasks()
{
    if(m_TaskRule.isNull()) return;

    QDateTime pCurrentDateTime = QDateTime::currentDateTime();
    QString pCurrentDateTimeStr;
    QDate pCurrentDate = pCurrentDateTime.date();
    QTime pCurrentTime = pCurrentDateTime.time();

    bool isOk = true;

    // 先检查月
    if(!m_TaskRule.date.isEmpty())
    {
        QVector<qint32>::iterator iter = qFind(m_TaskRule.date.begin(),
                                               m_TaskRule.date.end(),
                                               pCurrentDate.daysInMonth());
        if(iter == m_TaskRule.date.end())
            isOk = false;
        else
            pCurrentDateTimeStr += QString::asprintf("%d",pCurrentDate.daysInMonth());
    }

    // 检查周
    if(!m_TaskRule.dayOfWeek.isEmpty())
    {
        QVector<qint32>::iterator iter = qFind(m_TaskRule.dayOfWeek.begin(),
                                               m_TaskRule.dayOfWeek.end(),
                                               pCurrentDate.dayOfWeek());
        if(iter == m_TaskRule.dayOfWeek.end())
            isOk = false;
        else
            pCurrentDateTimeStr += QString::asprintf("%d",pCurrentDate.dayOfWeek());
    }

    // 检查小时
    if(!m_TaskRule.hour.isEmpty())
    {
        QVector<qint32>::iterator iter = qFind(m_TaskRule.hour.begin(),
                                               m_TaskRule.hour.end(),
                                               pCurrentTime.hour());
        if(iter == m_TaskRule.hour.end())
            isOk = false;
        else
            pCurrentDateTimeStr += QString::asprintf("%d",pCurrentTime.hour());
    }

    // 检查分钟
    if(!m_TaskRule.minute.isEmpty())
    {
        QVector<qint32>::iterator iter = qFind(m_TaskRule.minute.begin(),
                                               m_TaskRule.minute.end(),
                                               pCurrentTime.minute());
        if(iter == m_TaskRule.minute.end())
            isOk = false;
        else
            pCurrentDateTimeStr += QString::asprintf("%d",pCurrentTime.minute());
    }

    // 检查秒
    if(!m_TaskRule.second.isEmpty())
    {
        QVector<qint32>::iterator iter = qFind(m_TaskRule.second.begin(),
                                               m_TaskRule.second.end(),
                                               pCurrentTime.second());
        if(iter == m_TaskRule.second.end())
            isOk = false;
        else
            pCurrentDateTimeStr += QString::asprintf("%d",pCurrentTime.second());
    }

    if(isOk &&
       m_TaskRule.runCount > 0 &&
       m_currentRunCount >= m_TaskRule.runCount &&
       pCurrentDateTimeStr == m_CurrentDateTimeStr)
        isOk = false;

    if(pCurrentDateTimeStr != m_CurrentDateTimeStr)
        m_currentRunCount = 0;

    m_CurrentDateTimeStr=pCurrentDateTimeStr;

    if(isOk)
    {
        emit signal_timesatisfied();
        m_currentRunCount+=1;
    }
}
